package com.dlu.safe_construction.controller;

import com.dlu.safe_construction.common.ApiResult;
import com.dlu.safe_construction.dto.CarInfoDTO;
import com.dlu.safe_construction.model.Car;
import com.dlu.safe_construction.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by  lijiaxing on 2022/3/30 20:23
 *
 * @author lijiaxing
 */
@CrossOrigin
@RestController
@RequestMapping("/car")
@Slf4j
public class CarController implements BaseController {
    @Resource
    private CarService carService;

    @PostMapping(path = "/add.json", produces = APPLICATION_JSON)
    public ApiResult<Void> add(@RequestBody Car car) {
        return carService.add(car);
    }

    @GetMapping(path = "/delete.json", produces = APPLICATION_JSON)
    public ApiResult<Void> delete(String key, Integer byWhat) {
        return carService.deleteBy(key, byWhat);
    }

    @PostMapping(path = "/edit.json", produces = APPLICATION_JSON)
    public ApiResult<Void> edit(@RequestBody Car car) {
        return carService.edit(car);
    }

    @GetMapping(path = "/list.json", produces = APPLICATION_JSON)
    public ApiResult<List<CarInfoDTO>> list() {
        return carService.listAll();
    }

    @GetMapping(value = "/listBy.json", produces = APPLICATION_JSON)
    public ApiResult<List<CarInfoDTO>> listBy(String key, Integer byWhat) {
        return carService.searchBy(key, byWhat);
    }
}
